let requestUrl;

function addHandler() {
	let roleId = $("#role_id").combobox("getValue");

	if (roleId) {
		$("#roleId").combobox("setValue", roleId);
	}

	requestUrl = "/trade_record/insert";

	$("#record_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#record_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/trade_record/updateById";

		$("#id").val(rowData.id);
		$("#price").numberspinner("setValue", rowData.price);
		$("#roleId").combobox("setValue", rowData.roleId);
		$("#dataId").textbox("setValue", rowData.dataId);
		$("#tableName").combobox("setValue", rowData.tableName);
		$("#tradeTime").datebox("setValue", rowData.tradeTime);
		$("#tradeType").combobox("setValue", rowData.tradeType);
		$("#crossSever").combobox("setValue", rowData.crossSever);
		$("#designated").combobox("setValue", rowData.designated);

		$("#record_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录", "warning");
	}
}

function deleteHandler(id) {
	$.messager.confirm("提示", "是否确认删除？", function(bool) {
		if (bool) {
			ajaxPost("/trade_record/deleteById", {
				id: id
			}, function(resp) {
				showMsg(resp.message);

				$("#record_list").datagrid("reload");
			}, error);
		}
	});
}

$(document).ready(function() {

	$("#role_id").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: false,
		prompt: "-选择角色-",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#modeling_" + row.id).attr("src", data.image);
			}, error);

			return "<img width='18' id='modeling_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	$("#from").datebox({
		width: 150,
		prompt: "选择起始时间"
	});

	$("#to").datebox({
		width: 150,
		prompt: "选择截止时间"
	});

	$("#trade_type").combobox({
		url: "/trade_type/selectByType?type=1",
		valueField: "id",
		textField: "name",
		width: 150,
		required: false,
		prompt: "-交易类型-"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let tradeType = $("#trade_type").combobox("getValue");
		let roleId = $("#role_id").combobox("getValue");
		let from = $("#from").datetimebox("getValue");
		let to = $("#to").datetimebox("getValue");

		$("#record_list").datagrid("reload", {
			tradeType: tradeType,
			roleId: roleId,
			from: from,
			to: to
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#price").numberspinner({
		min: 1000,
		width: 150,
		required: true
	});

	$("#dataId").textbox({
		width: 150,
		required: true
	});

	$("#tableName").combobox({
		url: "/table/selectAll",
		valueField: "name",
		textField: "name",
		width: 150,
		required: true,
		panelWidth: 300
	});

	fetchSelect("#tradeType", "/trade_type/selectByType?type=1", {
		width: 150,
		required: true
	});
	fetchSelect("#crossSever", "/trade_type/selectByType?type=2", {
		width: 150,
		required: true
	});
	fetchSelect("#designated", "/trade_type/selectByType?type=3", {
		width: 150,
		required: true
	});
	fetchRoleId(150);

	$("#tradeTime").datebox({
		width: 150,
		required: true
	});

	$("#record_dialog").dialog({
		title: "交易记录",
		width: 500,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#record_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#record_dialog").dialog("close");
						$("#record_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#record_form").form("clear");
				$("#record_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_role_details * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#record_list").datagrid({
		url: "/trade_record/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		remoteSort: true,
		rownumbers: true,
		pagination: true,
		onHeaderContextMenu: function(e) {
			e.preventDefault();

			if (!columnMenu){
				createColumnMenu("#record_list");
			}
			columnMenu.menu("show", {
				left: e.pageX,
				top: e.pageY
			});
		},
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}],
		columns: [[
			{field: "id", hidden: true},
			{field: "tradeType", title: "交易类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/trade_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#trade_type_" + rowIndex).html(data.name);
					}, error);

					return "<div id='trade_type_" + rowIndex + "'></div>";
				}
			},
			{field: "crossSever", title: "交易类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/trade_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#cross_" + rowIndex).html(data.name);
					}, error);

					return "<div id='cross_" + rowIndex + "'></div>";
				}
			},
			{field: "designated", title: "交易类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/trade_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#design_" + rowIndex).html(data.name);
					}, error);

					return "<div id='design_" + rowIndex + "'></div>";
				}
			},
			{field: "price", title: "交易价格", align:"center", width: 100, sortable: true},
			{field: "tableName", title: "数据表名", align:"center", width: 100},
			{field: "dataId", title: "数据编号", align:"center", width: 100},
			{field: "tradeTime", title: "交易时间", align: "center", width: 100, sortable: true},
			roleDetailsColumn, deleteColumn
		]]
	});

});